﻿// ***********************************************************************
// Project			: Micua
// Assembly         : Micua.BLL
// Author           : iceStone
// Created          : 2013-11-23 21:59
//
// Last Modified By : iceStone
// Last Modified On : 2013-11-23 22:06
// ***********************************************************************
// <copyright file="OptionService.cs" company="Wedn.Net">
//     Copyright (c) Wedn.Net. All rights reserved.
// </copyright>
// <summary>配置信息管理拓展类</summary>
// ***********************************************************************

using System.Collections.Generic;
using System.Linq;
using Micua.Core.Configuration;
using Micua.Model;

namespace Micua.BLL
{
    /// <summary>
    /// 配置信息管理拓展类
    /// </summary>
    /// <remarks>
    ///  2013-11-23 22:09 Created By iceStone
    /// </remarks>
    public partial class OptionService
    {
        /// <summary>
        /// 获取设置信息字典
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:09 Created By iceStone
        /// </remarks>
        /// <returns>设置信息字典</returns>
        public IDictionary<string, string> GetSetting()
        {
            return Query(o => o.Enabled).Select(o => new { o.Name, o.Value }).ToDictionary(o => o.Name, o => o.Value);
            //IDictionary<string, string> dict = new Dictionary<string, string>();
            //if (list.Count == 0) return dict;
            //foreach (var item in list)
            //{
            //    dict.Add(item.Name, item.Value);
            //}
            //return dict;
        }

        /// <summary>
        /// 向数据库表中插入一个对象记录
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:09 Created By iceStone
        /// </remarks>
        /// <param name="entity">要插入的实体对象</param>
        /// <returns>插入完成的实体</returns>
        public override Option Insert(Option entity)
        {
            Setting.Set(entity.Name, entity.Value);
            return base.Insert(entity);
        }
        /// <summary>
        /// 向数据库中插入一条数据
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:09 Created By iceStone
        /// </remarks>
        /// <param name="entity">要插入的实体</param>
        /// <param name="commit">是否提交到数据库</param>
        /// <returns>当前实体</returns>
        public override Option Insert(Option entity, bool commit)
        {
            Setting.Set(entity.Name, entity.Value);
            return base.Insert(entity, commit);
        }
        /// <summary>
        /// 向数据库中插入多条数据
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:09 Created By iceStone
        /// </remarks>
        /// <param name="entitys">要插入的实体</param>
        /// <returns>当前实体</returns>
        public override IList<Option> Insert(params Option[] entitys)
        {
            foreach (var entity in entitys)
            {
                Setting.Set(entity.Name, entity.Value);
            }
            return base.Insert(entitys);
        }
        /// <summary>
        /// 向数据库中插入多条数据
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:09 Created By iceStone
        /// </remarks>
        /// <param name="commit">是否提交到数据库</param>
        /// <param name="entitys">要插入的实体</param>
        /// <returns>当前实体</returns>
        public override IList<Option> Insert(bool commit, params Option[] entitys)
        {
            foreach (var entity in entitys)
            {
                Setting.Set(entity.Name, entity.Value);
            }
            return base.Insert(commit, entitys);
        }

        /// <summary>
        /// 根据选项名称更新对应的选项值, 返回执行结果受影响行数
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:09 Created By iceStone
        /// </remarks>
        /// <param name="name">要修改的选项名称</param>
        /// <param name="value">要更新的值</param>
        /// <returns>执行结果受影响行数</returns>
        public int UpdateValue(string name, string value)
        {
            Option entity = Repository.QuerySingle(o => o.Name == name);
            entity.Value = value;
            int row = Update(entity);
            //更新缓存
            Setting.Set(name, value);
            return row;
        }
        /// <summary>
        /// Updates the specified entity.
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:09 Created By iceStone
        /// </remarks>
        /// <param name="entity">The entity.</param>
        /// <returns>System.Int32.</returns>
        public override int Update(Option entity)
        {
            Setting.Set(entity.Name, entity.Value);
            return base.Update(entity);
        }
        /// <summary>
        /// Updates the specified entity.
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:09 Created By iceStone
        /// </remarks>
        /// <param name="entity">The entity.</param>
        /// <param name="commit">if set to <c>true</c> [commit].</param>
        /// <returns>System.Int32.</returns>
        public override int Update(Option entity, bool commit)
        {
            Setting.Set(entity.Name, entity.Value);
            return base.Update(entity, commit);
        }
        /// <summary>
        /// Updates the specified entitys.
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:09 Created By iceStone
        /// </remarks>
        /// <param name="entitys">The entitys.</param>
        /// <returns>System.Int32.</returns>
        public override int Update(params Option[] entitys)
        {
            foreach (var entity in entitys)
            {
                Setting.Set(entity.Name, entity.Value);
            }
            return base.Update(entitys);
        }
        /// <summary>
        /// Updates the specified commit.
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:09 Created By iceStone
        /// </remarks>
        /// <param name="commit">if set to <c>true</c> [commit].</param>
        /// <param name="entitys">The entitys.</param>
        /// <returns>System.Int32.</returns>
        public override int Update(bool commit, params Option[] entitys)
        {
            foreach (var entity in entitys)
            {
                Setting.Set(entity.Name, entity.Value);
            }
            return base.Update(commit, entitys);
        }
        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:09 Created By iceStone
        /// </remarks>
        /// <param name="entity">The entity.</param>
        /// <returns>System.Int32.</returns>
        public override int Delete(Option entity)
        {
            Setting.Remove(entity.Name);
            return base.Delete(entity);
        }
        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:09 Created By iceStone
        /// </remarks>
        /// <param name="entity">The entity.</param>
        /// <param name="commit">if set to <c>true</c> [commit].</param>
        /// <returns>System.Int32.</returns>
        public override int Delete(Option entity, bool commit)
        {
            Setting.Remove(entity.Name);
            return base.Delete(entity, commit);
        }
        /// <summary>
        /// Deletes the specified ids.
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:09 Created By iceStone
        /// </remarks>
        /// <param name="ids">The ids.</param>
        /// <returns>System.Int32.</returns>
        public override int Delete(params int[] ids)
        {
            int res = base.Delete(ids);
            Setting.Init(GetSetting());
            return res;
        }
        /// <summary>
        /// Deletes the specified commit.
        /// </summary>
        /// <remarks>
        ///  2013-11-23 22:09 Created By iceStone
        /// </remarks>
        /// <param name="commit">if set to <c>true</c> [commit].</param>
        /// <param name="ids">The ids.</param>
        /// <returns>System.Int32.</returns>
        public override int Delete(bool commit, params int[] ids)
        {
            int res = base.Delete(commit, ids);
            Setting.Init(GetSetting());
            return res;
        }
    }
}
